package com.mths.client.widget;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.mths.client.component.Cell;
import com.mths.client.component.ComboBox;
import com.mths.client.component.DateField;
import com.mths.client.component.Div;
import com.mths.client.component.Label;
import com.mths.client.component.NumberField;
import com.mths.client.component.Row;
import com.mths.client.component.Table;
import com.mths.client.component.TextField;
import com.mths.client.constant.DocumentConstant;
import com.mths.client.constant.FieldType;
import com.mths.client.dto.MetaDataDTO;
import com.mths.client.dto.MetaParamDTO;
import com.mths.client.util.ClientCacheUtil;

public class DocumentMetaWidget extends Composite {

	private static DocumentMetaWidgetUiBinder	uiBinder	= GWT.create(DocumentMetaWidgetUiBinder.class);

	interface DocumentMetaWidgetUiBinder extends UiBinder<Widget, DocumentMetaWidget> {
	}

	private UploadFile	uploadFileCmp;

	@UiField
	Div					widgetDiv;

	public DocumentMetaWidget() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void loadWidgetComponent(List<MetaDataDTO> result) {
		if (result == null || result.isEmpty()) {
			GWT.log("DocumentMetaWidget loadWidgetComponent result null or empty");
			return;
		}
		Table table = new Table();
		table.setWidth("100%");
		Row row = null;
		Cell cell;
		for (MetaDataDTO metaData : result) {

			if (result.indexOf(metaData) % 2 == 0) {
				row = new Row();
			}

			cell = new Cell();
			Label label = new Label(metaData.getFieldName());
			cell.add(label);
			row.add(cell);

			cell = new Cell();
			if (FieldType.NUMBER_FIELD.equals(metaData.getFieldType())) {
				NumberField numberField = new NumberField();
				numberField.setComponentId(metaData.getFieldId());
				cell.add(numberField);
			} else if (FieldType.TEXT_FIELD.equals(metaData.getFieldType())) {
				TextField textField = new TextField();
				textField.setComponentId(metaData.getFieldId());
				cell.add(textField);
			} else if (FieldType.COMBOBOX_FIELD.equals(metaData.getFieldType())) {
				ComboBox comboBox = new ComboBox(false);
				comboBox.setComponentId(metaData.getFieldId());
				if (metaData.getMetaParamList() != null) {
					for (MetaParamDTO metaParam : metaData.getMetaParamList()) {
						comboBox.addItem(metaParam.getValue(), metaParam.getKey(), false);
					}
				}
				cell.add(comboBox);
			} else if (FieldType.DATE_FIELD.equals(metaData.getFieldType())) {
				DateField dateField = new DateField(true);
				dateField.setComponentId(metaData.getFieldId());
				cell.add(dateField);
			}

			row.add(cell);

			if (result.indexOf(metaData) % 2 == 0) {
				table.add(row);
			}
		}

		// user standart component - user-createdate
		row = new Row();

		cell = new Cell();
		Label userLbl = new Label("Kullanıcı");
		cell.add(userLbl);
		row.add(cell);

		cell = new Cell();
		TextField userTxt = new TextField();
		userTxt.setComponentId(DocumentConstant.UPLOAD_USER);
		userTxt.setFieldValue(ClientCacheUtil.instance().getSessionUser().getFullName());
		userTxt.setEnabled(false);
		cell.add(userTxt);
		row.add(cell);

		cell = new Cell();
		Label processLbl = new Label("İşlem Tarihi");
		cell.add(processLbl);
		row.add(cell);

		cell = new Cell();
		DateField processDate = new DateField(true);
		processDate.setEnabled(false);
		processDate.setFieldValue(new Date());
		processDate.setComponentId(DocumentConstant.PROCESS_DATE);
		cell.add(processDate);
		row.add(cell);

		table.add(row);

		// add file upload component

		row = new Row();

		cell = new Cell();
		Label fileUploadLbl = new Label("Dosya Yükleme");
		cell.add(fileUploadLbl);
		row.add(cell);

		cell = new Cell();
		cell.setColSpan("3");
		uploadFileCmp = new UploadFile();
		cell.add(uploadFileCmp);
		row.add(cell);

		table.add(row);

		widgetDiv.add(table);
	}

	public Map<String, String> getFieldsValueMap() {
		Map<String, String> fieldsValue = new HashMap<String, String>();
		iterateOverWidget(fieldsValue, widgetDiv);
		// these two meta are constant override the screen value
		fieldsValue.put(DocumentConstant.UPLOAD_USER, ClientCacheUtil.instance().getSessionUser().getObjid());
		fieldsValue.put(DocumentConstant.PROCESS_DATE, DateTimeFormat.getFormat(DocumentConstant.META_DATE_TIME_FORMAT).format(new Date()));

		return fieldsValue;
	}

	private void iterateOverWidget(Map<String, String> fieldsValue, ComplexPanel component) {
		Iterator<Widget> iterator = component.iterator();
		while (iterator.hasNext()) {
			Widget widget = iterator.next();
			if (widget instanceof NumberField) {
				NumberField numberField = (NumberField) widget;
				fieldsValue.put(numberField.getComponentId(), numberField.getFieldValue());
			} else if (widget instanceof TextField) {
				TextField textField = (TextField) widget;
				fieldsValue.put(textField.getComponentId(), textField.getFieldValue());
			} else if (widget instanceof ComboBox) {
				ComboBox comboBox = (ComboBox) widget;
				if (!comboBox.isMultipleSelect())
					fieldsValue.put(comboBox.getComponentId(), comboBox.getFieldValue());
				else
					new MessageBox("Beklenmeyen bir data olustu, kontrol ediniz");
			} else if (widget instanceof DateField) {
				DateField dateField = (DateField) widget;
				fieldsValue.put(dateField.getComponentId(), dateField.getFieldValueFormat());
			} else if (widget instanceof Label) {
				// continue
			} else if (widget instanceof UploadFile) {
				// continue
			} else if (((ComplexPanel) widget).iterator() != null) {
				iterateOverWidget(fieldsValue, (ComplexPanel) widget);
			}
		}
	}

	public void clear() {
		widgetDiv.clear();
	}

	public UploadFile getUploadFileCmp() {
		return uploadFileCmp;
	}
}
